﻿using System;
using System.Windows.Forms;
using Business;
using Business.Dicionary;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Entity.Sped;
using Business.Forms;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Sped
{
    public partial class FormNcm : FormDefault, IForm
    {
        public FormNcm()
        {
            InitializeComponent();
            situacaoTributariaDicionaryBindingSource.DataSource = new SituacaoTributariaDicionary();
            situacaoTributariaDicionaryBindingSource1.DataSource = new SituacaoTributariaDicionary();
            situacaoTributariaDicionaryBindingSource2.DataSource = new SituacaoTributariaDicionary();
        }

        public object Novo()
        {
            MessageBox.Show("Não é possível inserir um novo NCM.");
            return null;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            ShowDialog();
        }

        public object Save()
        {
            if (!Validate().HasError)
            {
                NcmRepository.Save((Ncm)CurrentObject);
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            MessageBox.Show("Não é possível excluir um NCM.");
            return false;
        }

        public object CurrentObject
        {
            get
            {
                ncmBindingSource.EndEdit();
                return ncmBindingSource.DataSource;
            }
            set
            {
                ncmBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return Business.Seguranca.SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            var ncm = (Ncm)CurrentObject;
            var result = DataValidation.ValidateEntity(ncm);
            foreach (var error in DataValidation.ValidateEntity(ncm.Tributacao).ValidationErrors)
            {
                result.ValidationErrors.Add(error);
            }
            return result;
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}
